package com.frxuexi.park2212.park

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.base.BaseActivity
import com.frxuexi.lib_base.entity.ParkBeanItem
import com.frxuexi.park2212.R
import com.frxuexi.park2212.databinding.ActivityParkAddBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/ParkAddActivity")
@AndroidEntryPoint
class ParkAddActivity : BaseActivity<ActivityParkAddBinding,ParkViewModel>() {
    var area=""
    var type=""
    override fun initData() {
        vm.addPark.observe(this){
            ToastUtils.showLong("添加成功")
            finish()
        }
    }

    override fun initView() {
        binding.headParkAdd.headerTitle.setText("添加车位")
        binding.headParkAdd.headerAdd.apply {
            setImageResource(R.drawable.ic_action_ok)
            setOnClickListener {
                val remark = binding.parkaddRemark.text.toString()
                val carNo = binding.parkaddCarNo.text.toString()
                val item = ParkBeanItem(
                    3, area, null, "", remark,
                    carNo, "空闲", type, 0, ""
                )

                vm.addPark(item)
            }
        }

        val arr = arrayOf("A区停车场", "B区停车场", "C区停车场")
        val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, arr)
        binding.parkaddArea.adapter=adapter
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.parkaddArea.onItemSelectedListener=object :OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                area=arr[position]
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }

        }

        val arrType = arrayOf("标准车位", "VIP车位")
        val adapter2 = ArrayAdapter(this, android.R.layout.simple_spinner_item, arrType)
        binding.parkaddType.adapter=adapter2
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.parkaddType.onItemSelectedListener=object :OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                type=arrType[position]

            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }

        }

        binding.parkaddRemark.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                val length = s?.length
                binding.parkaddNum.setText("${length}/10")
                if(length==10){
                    binding.parkaddNum.setTextColor(Color.RED)
                }else{
                    binding.parkaddNum.setTextColor(Color.GRAY)
                }
            }

            override fun afterTextChanged(s: Editable?) {
            }

        })
    }

}